﻿using Projecturo.Model;
using System.Collections.ObjectModel;
using Neits.PersistenceService.Providers;
using Projecturo.ProjecturoClient.Connection;

namespace Projecturo.ProjecturoClient.ViewModel.Project
{
    public class TimeRecordListViewModel : WorkspaceViewModel
    {
        private Ticket ticket;
        private ObservableCollection<TimeRecord> timeRecords;

        public override string DisplayName
        {
            get
            {
                return "Time Records";
            }
            protected set
            {
                base.DisplayName = value;
            }
        }

        public ObservableCollection<TimeRecord> TimeRecords
        {
            get
            {
                if (timeRecords == null)
                    LoadTimeRecords();
                return timeRecords;
            }
        }

        public override void SetParent(object parent)
        {
            ticket = parent as Ticket;
        }

        private void LoadTimeRecords()
        {
            if (ticket != null)
            {
                if (timeRecords == null)
                    timeRecords = new ObservableCollection<TimeRecord>();
                else
                    timeRecords.Clear();

                var filter = new HqlFilter("from TimeRecord tr where tr.Ticket.Id = " + ticket.Id);
                var records = Connector.Connectivity.Storage.GetList<TimeRecord>(filter);
                foreach (var record in records)
                {
                    timeRecords.Add(record);
                }
            }
        }
    }
}
